//
//  WWAletView.m
//  RONGAO
//
//  Created by wei wang on 2017/3/13.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "WWAletView.h"
#import "IOTCamera/IOTCAPIs.h"
@interface WWAletView()<UITableViewDelegate,UITableViewDataSource,MyCameraDelegate>{
    
    SWifiAp wifiSSIDList[28];
    int wifiSSIDListCount;
    int isclick;
    char mode;
    char enctype;
}


@property (nonatomic,strong)UIView *containerView;
@property (nonatomic,strong)UIView *topView;


@property (nonatomic,strong)UIView *topcontainerView;
@property (nonatomic,strong)UIView *bottomcontainerView;

/** image **/
@property(nonatomic,strong) UIImageView *rightimage;
/** 上面的容器 **/
@property(nonatomic,strong) UIView *topbgView;

/** 下面的容器 **/
@property(nonatomic,strong) UIView *bottombgView;

/** btn **/
@property(nonatomic,strong) UIButton *btn;


@end

@implementation WWAletView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        wifiSSIDListCount = -1;
        isclick = -1;
        self.backgroundColor = [UIColor colorWithRed:167/255.0 green:163/255.0 blue:161/255.0 alpha:0.8f];
        UISwipeGestureRecognizer* recognizer;
        // handleSwipeFrom 是偵測到手势，所要呼叫的方法
        recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
        // 不同的 Recognizer 有不同的实体变数
        // 例如 SwipeGesture 可以指定方向
        // 而 TapGesture 則可以指定次數
        [self addGestureRecognizer:recognizer];
        
        
        [self configView];
    }
    return self;
}
- (void)handleSwipeFrom:(UISwipeGestureRecognizer*)recognizer {
    // 触发手勢事件后，在这里作些事情
    // 底下是刪除手势的方法
    [self cancelClick:nil];
    
    
    
    
}
- (instancetype)init{
    self = [super init];
    if (self) {
        
        [self configView];
        
    }
    
    return self;
}


- (void)dealloc{
    
    
}
- (void)reloadTableView:(SMsgAVIoctrlListWifiApResp *)s typeSize:(NSUInteger)size listcount:(int)count{

    wifiSSIDListCount = count;

    memcpy(wifiSSIDList, s->stWifiAp, size - sizeof(s->number));
    [self.tableView reloadData];
}

- (void)configView{
    
    __weak typeof(self) weakSelf = self;
    __weak UIView *superView = self;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.mas_left).mas_offset(30);
        make.right.equalTo(weakSelf.mas_right).mas_offset(-30);
        make.centerY.equalTo(weakSelf.mas_centerY).mas_offset(-60);
        make.height.mas_equalTo(pxScaleHeight(740));
    }];
    
    self.containerView.layer.masksToBounds = YES;
    self.containerView.layer.cornerRadius = 10.0;
    
    
    
    
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView);
        make.right.equalTo(weakSelf.containerView);
        make.height.mas_equalTo(pxScaleHeight(120));
    }];
    
   
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.topView);
        make.top.equalTo(weakSelf.topView);
        make.right.equalTo(weakSelf.topView);
        make.height.equalTo(weakSelf.topView);
    }];

    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(weakSelf.containerView.mas_right);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.mas_equalTo(50);
        make.width.equalTo(weakSelf.cancelBtn);
        
    }];
    
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.sureBtn.mas_left);
        make.left.equalTo(weakSelf.containerView.mas_left);
        make.bottom.equalTo(weakSelf.containerView.mas_bottom);
        make.height.equalTo(weakSelf.sureBtn);
    }];
    
   [self.topcontainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.containerView.mas_right);
        make.left.mas_equalTo(weakSelf.containerView.mas_left);
        make.top.mas_equalTo(weakSelf.topView.mas_bottom);
        make.bottom.mas_equalTo(weakSelf.bottomcontainerView.mas_top);
    }];
    
    [self.bottomcontainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(weakSelf.containerView.mas_right);
        make.left.mas_equalTo(weakSelf.containerView.mas_left);
        make.bottom.mas_equalTo(weakSelf.sureBtn.mas_top);
        make.height.mas_equalTo(weakSelf.topcontainerView);
        
    }];

    [self.wifilabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(weakSelf.topcontainerView.mas_left).mas_offset(20);
        make.top.mas_equalTo(weakSelf.topcontainerView.mas_top).mas_offset(6);
        
    }];
    
    
    [self.topbgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(weakSelf.topcontainerView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.topcontainerView.mas_centerY).mas_offset(10);
        make.width.mas_equalTo(pxScaleWidth(670));
        make.height.mas_equalTo(pxScaleHeight(100));
    }];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.topbgView.mas_bottom);
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(pxScaleWidth(670));
        make.bottom.mas_equalTo(superView.mas_bottom).mas_offset(-100);
        
    }];
    [self.selectwifilabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(weakSelf.topbgView.mas_centerY);
        make.left.mas_equalTo(weakSelf.topbgView.mas_left).mas_offset(10);
        
    }];
    
    [self.pwdlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.bottomcontainerView.mas_left).mas_offset(20);
        make.top.mas_equalTo(weakSelf.bottomcontainerView.mas_top).mas_offset(6);
        
    }];
    
    [self.bottombgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(weakSelf.bottomcontainerView.mas_centerX);
        make.centerY.mas_equalTo(weakSelf.bottomcontainerView.mas_centerY).mas_offset(10);
        make.width.mas_equalTo(pxScaleWidth(670));
        make.height.mas_equalTo(pxScaleHeight(100));
    }];
    
    [self.rightimage mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.right.mas_equalTo(weakSelf.topbgView.mas_right).mas_offset(-10);
        make.centerY.mas_equalTo(weakSelf.topbgView.mas_centerY);
        make.width.and.height.mas_equalTo(8);
    }];
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(weakSelf.topcontainerView);
        
    }];
    [self.passwordfile mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(weakSelf.bottombgView.mas_left).mas_offset(10);
        make.top.mas_equalTo(weakSelf.bottombgView.mas_top);
        make.bottom.mas_equalTo(weakSelf.bottombgView.mas_bottom);
        make.right.mas_equalTo(weakSelf.bottombgView.mas_right);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    if (self.pressCancelBtn) {
        self.pressCancelBtn();
    }

}
#pragma makr - 点击事件
- (void)sureClick:(UIButton *)sender{
    
    if ([self.passwordfile.text length]>0) {
        if (self.pressSureBtn) {
            self.pressSureBtn(self.selectwifilabel.text,self.passwordfile.text,mode,enctype);
        }
    }else{
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
    }

}


- (void)cancelClick:(UIButton*)sender{
    
    NSLog(@"点击了取消");
    [self endEditing:YES];
    
    
    if (self.pressCancelBtn) {
        self.pressCancelBtn();
    }
}

- (void)tableViewshow:(UIButton *)sender{
    isclick = isclick+1;
    
    NSLog(@"点击了tableview");
    if (isclick%2==0) {
        
        self.tableView.hidden = NO;
        
    }else{
        
        self.tableView.hidden = YES;
    }

}

#pragma mark - 动画
- (void)outWithAnimation{
    
    typeof (self)weakSelf = self;
    [UIView animateWithDuration:.35f animations:^{
        CGRect frame =  weakSelf.frame;
        frame.origin.y = weakSelf.frame.size.height;
        weakSelf.frame = frame;
        
    } completion:^(BOOL finished) {
        if (finished) {
            [weakSelf removeFromSuperview];
            
        }
    }];
    
    
    
}


#pragma mark - tableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return wifiSSIDList != NULL && wifiSSIDListCount >= 0 ? wifiSSIDListCount : 0;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
    
}
//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//    
//    if (wifiSSIDListCount < 0)
//        return NSLocalizedString(@"Scanning...", @"");
//    else if (wifiSSIDListCount == 0)
//        return NSLocalizedString(@"No Wi-Fi network found", @"");
//    else
//        return NSLocalizedString(@"Choose a Network...", @"");
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}

#pragma mark - tableView Delegate
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14.f]];
    SWifiAp wifiAp = wifiSSIDList[indexPath.row];
    
    NSLog(@"wifi AP: %s \t%d", wifiAp.ssid, wifiAp.status);
    
    NSString *ssid = [NSString stringWithUTF8String: wifiAp.ssid];
    
    if ([ssid length] > 0)
        cell.textLabel.text = ssid;
    else
        cell.textLabel.text = NSLocalizedString(@"Unknown", @"");
    
    cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SWifiAp wifiAp = wifiSSIDList[indexPath.row];
    
    NSLog(@"wifi AP: %s \t%d", wifiAp.ssid, wifiAp.status);
    
    NSString *ssid = [NSString stringWithUTF8String: wifiAp.ssid];
    self.selectwifilabel.text = ssid;
    mode = wifiAp.mode;
    enctype = wifiAp.enctype;
    self.tableView.hidden = YES;
    isclick = isclick+1;
}


- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.hidden = YES;
    }
    
    return _tableView;
    
}
- (UIView *)topView{
    if (!_topView) {
        
        _topView = [[UIView alloc]init];
        _topView.backgroundColor = [UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f];
        [self.containerView addSubview:self.topView];
    }
    return _topView;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];;
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont systemFontOfSize:17];
        [self.topView addSubview:self.titleLabel];
    }
    return _titleLabel;
}

- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor colorWithRed:217/255.0 green:183/255.0 blue:108/255.0 alpha:1.0f] forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureClick:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.layer.borderWidth = .3f;
        _sureBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [self.containerView addSubview:_sureBtn];
        
    }
    return _sureBtn;
}

- (UIButton *)cancelBtn
{
    if (!_cancelBtn) {
        _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cancelBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        _cancelBtn.layer.borderWidth = .3f;
        _cancelBtn.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        [_cancelBtn addTarget:self action:@selector(cancelClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:_cancelBtn];
    }
    return _cancelBtn;
}

- (UILabel *)wifilabel{
    
    if (!_wifilabel) {
        
        _wifilabel = [[UILabel alloc]init];;
        _wifilabel.textColor = [UIColor grayColor];
        _wifilabel.font = [UIFont systemFontOfSize:13];
        _wifilabel.text = @"WiFi名称";
        [self.topcontainerView addSubview:_wifilabel];
        
    }
   
    return _wifilabel;
}
- (UILabel *)pwdlabel{
    
    if (!_pwdlabel) {
        
        _pwdlabel = [[UILabel alloc]init];;
        _pwdlabel.textColor = [UIColor grayColor];
        _pwdlabel.font = [UIFont systemFontOfSize:13];
        _pwdlabel.text = @"密码";
        [self.bottomcontainerView addSubview:_pwdlabel];
        
    }
    
    return _pwdlabel;
}

- (UIView *)containerView{
    
    if (!_containerView) {
        _containerView = [[UIView alloc] init];
        _containerView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_containerView];
    }
    
    return _containerView;
}

- (UIView *)topcontainerView{
    
    if (!_topcontainerView) {
        _topcontainerView = [[UIView alloc] init];
        [self.containerView addSubview:_topcontainerView];

    }
    return _topcontainerView;
}

- (UIView *)bottomcontainerView{
    
    if (!_bottomcontainerView) {
        
        _bottomcontainerView = [[UIView alloc] init];
        [self.containerView addSubview:_bottomcontainerView];

    }
    return _bottomcontainerView;
}


- (UIView *)topbgView{
    
    if (!_topbgView) {
        
        _topbgView = [[UIView alloc] init];
        
        _topbgView.layer.masksToBounds = YES;
        _topbgView.layer.borderWidth = 0.4f;
        _topbgView.layer.borderColor = [UIColor grayColor].CGColor;
        
        [self.topcontainerView addSubview:_topbgView];
    }
    
    return _topbgView;
}

- (UIButton *)btn{
    
    if (!_btn) {
        
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn addTarget:self action:@selector(tableViewshow:) forControlEvents:UIControlEventTouchUpInside];
        [self.topcontainerView addSubview:_btn];
    }
    return _btn;
}
- (UIView *)bottombgView{
    
    if (!_bottombgView) {
        
        _bottombgView = [[UIView alloc] init];
        
        _bottombgView.layer.masksToBounds = YES;
        _bottombgView.layer.borderWidth = 0.4f;
        _bottombgView.layer.borderColor = [UIColor grayColor].CGColor;
        [self.bottomcontainerView addSubview:_bottombgView];
    }
    
    return _bottombgView;
}

- (UIImageView *)rightimage{
    
    if (!_rightimage) {
        
        _rightimage = [[UIImageView alloc] init];
        [_rightimage setImage:[UIImage imageNamed:@"set_icon_more"]];
        [self.topbgView addSubview:_rightimage];
    }
    return _rightimage;
}
- (UILabel *)selectwifilabel{
    
    if (!_selectwifilabel) {
        _selectwifilabel = [[UILabel alloc]init];;
        _selectwifilabel.textColor = [UIColor grayColor];
        _selectwifilabel.font = [UIFont systemFontOfSize:13];
        [self.topbgView addSubview:_selectwifilabel];
    }
    return _selectwifilabel;
}

- (UITextField *)passwordfile{
    
    if (!_passwordfile) {
        
        _passwordfile = [[UITextField alloc] init];
        _passwordfile.textColor = [UIColor grayColor];
        
        [_passwordfile setFont:[UIFont systemFontOfSize:14]];
        [self.bottombgView addSubview:_passwordfile];
    }
    
    return _passwordfile;
}

- (NSMutableArray *)wifiArray{
    
    if (!_wifiArray) {
        
        _wifiArray = [NSMutableArray array];
    }
    return _wifiArray;
}
@end
